package StudentManagementSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Begain {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> list = new ArrayList<User>();
        loop:
        while (true) {
            Menue();
            String choose = sc.next();
            switch (choose) {
                case "1" -> SignIn(list);//登录
                case "2"-> register(list);//注册
                case "3"-> fogetkey(list);//忘记密码
                case "4" ->{
                    System.out.println("关闭网页");
                    break loop;
                }

            }
        }
    }

    public static void Menue() {//登录界面
        System.out.println("@@@@@@@@@@@@@@@@@@@@欢迎来到学生管理系统登录界面@@@@@@@@@@@@@@@@@@@@");
        System.out.println("请选择您的操作：");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.忘记密码");
        System.out.println("4.关闭网页");
    }
    //登录开始
    public static void SignIn(ArrayList<User> list) {//登录开始
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账户名：");
        String username = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        String yanzhen = yanzhenma();
        int a = 1;
       m: while (true){
            if(a==3){
                System.out.println("多次输入未存在账户，退出登录界面");
                break ;
            }
            int conu = containsUsername(list,username);
        if (conu==-1){
            System.out.println("账户不存在，请重新输入账户:");
            username = sc.next();
            a++;
        }
        if(conu>=0){
            User u = list.get(conu);
            String realpassword =u.getPassword();
            for (int i = 0; i < 3; i++) {
                if (realpassword.equals(password)){
                    System.out.print("密码正确,请正确输入"+"("+yanzhen+")"+"验证码:");
                    while (true) {
                        String tryyanzhen = sc.next();
                        if (tryyanzhen.equalsIgnoreCase(yanzhen)) {
                            System.out.println("登录成功，进入学生管理界面!");
                            Main ss = new Main();
                            ss.start();
                            break;
                        } else {
                            yanzhen = yanzhenma();
                            System.out.println("验证码错误，请重新输入"+yanzhen);
                        }
                    }
                }else {
                    System.out.println("密码错误，您还有"+(2-i)+"次机会");
                }
                if (i==2){
                    System.out.println("3次尝试失败，该账户冻结一天");
                    break m;

                }
                password = sc.next();
            }
        }
      }
    }
    //判断用户名是否存在
    public static int containsUsername(ArrayList<User> list,String username){
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (username.equals(u.getUsername()))
                return i;
        }
        return -1;
    }
    //判断密码是否正确
    public static boolean containspassword(ArrayList<User> list,String password) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (password.equals(u.getPassword()))
                return true;
        }
        return false;
    }
    //验证码系统
    public static String yanzhenma(){
        ArrayList<Character> list = new ArrayList<>();
        StringBuilder sb =new StringBuilder();
        for (int i = 0; i < 26; i++) {
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int number = r.nextInt(10);
        sb.append(number);
        char[] arr = sb.toString().toCharArray();
        int randomnIndex = r.nextInt(arr.length);
        char temp = arr[randomnIndex];
        arr[randomnIndex] = arr[arr.length-1];
        arr[arr.length-1] = temp;
        return new String(arr);
    }
    //注册账号
    public static void register(ArrayList<User> list) {
        User u = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎来到注册界面");
        System.out.println("请输入您要注册的用户名：");
        while (true) {
            String username = sc.next();
            int number = containsUsername(list, username);
            if (number >= 0) {
                System.out.println("当前用户名已被注册，请重新输入用户名：");
            }
            else{
                u.setUsername(username);
                System.out.println("请输入您的密码：");
                String password = sc.next();
                u.setPassword(password);
                System.out.println("请输入您的手机号码");
                String phone = sc.next();
                u.setPhone(phone);
                System.out.println("请输入您的身份证号");
                String idcard = sc.next();
                u.setIdCard(idcard);
                list.add(u);
                System.out.println("账号注册成功！");
                break;
            }
        }
    }
    //忘记密码
    public static void fogetkey(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您忘掉密码的账户的名");
        while (true) {
            String fogetUsername = sc.next();
            int exit = containsUsername(list, fogetUsername);
            if (exit >= 0) {
                User u = list.get(exit);
                String realphone = u.getPhone();
                String realidcard = u.getIdCard();
                System.out.println("下面，请您输入忘掉密码的手机号与身份证号来方便验证");
                System.out.println("请输入手机号:");
                String fogetphone = sc.next();
                System.out.println("请输入身份证号:");
                String forgetidcard = sc.next();
                System.out.println("正在核验-----------");
                if (fogetphone.equals(realphone)&&forgetidcard.equals(realidcard)){
                    System.out.println("身份验证成功，您可以重置密码");
                    System.out.println("请输入您要重置的密码：");
                    String forgetkey = sc.next();
                    u.setPassword(forgetkey);
                    System.out.println("重置密码成功!");
                    break;
                }else {
                    System.out.println("身份验证失败,退出程序");
                    break;
                }
            } else {
                System.out.println("该用户名不存在，请重新输入:");
            }
        }
    }
}
